home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 116 / MacAddict 116 (Mac Power Pack)(theDISC)(April 2006).iso / Software / Interface / HolidayWidgetBundle.dmg / / Statosphere.wdgt / statosphere.js < prev    next >
Encoding:
JavaScript  |  2005-06-15  |  26.6 KB  |  904 lines

  1. var widgetIsShowing = true, widgetAtFront = true;
  2. var top_monitor=2, mid_monitor=0, bot_monitor=1; //setting initial conditions corresponding with the list below (changed if widget preferences set)
  3. var num_procs = 1; //this is for seph at macthemesforums ;)
  4.  
  5. /*
  6.     cpu = 0
  7.     ram = 1
  8.     network = 2
  9.     battery = 3
  10.     airport = 4
  11.     
  12.     basically, we print these out from the shell script in the order they
  13.     appear in the HTML, making life a bit easier to figure out which monitor
  14.     to use for the values...
  15.  */
  16.  
  17. var cli_monitors = ['c','r','n','b','a','d']; 
  18. var monitors = ['CPU Usage','RAM Usage','Network Load','Battery Level','Airport Strength','Startup Disk Usage']; 
  19.  
  20. function getSysInfo() {
  21.     if (window.widget && widgetIsShowing && widgetAtFront) {
  22.         //we set up the command line arguments to send the shell script:
  23.         var cli_args = "-" + cli_monitors[top_monitor] + cli_monitors[mid_monitor] + cli_monitors[bot_monitor];
  24.         var sys_info = widget.system("sysinfo.sh " + cli_args, null).outputString.split(","); //100 - (/(\d+)([\t\w]*)$/img.exec(sar)[0])*1;
  25.  
  26.         //alert('from script: ' + sys_info);
  27.  
  28.         var top_light_value =  Math.abs(Math.round(sys_info[top_monitor]*1)); //incase of a weird RAM thingy with dual procs... (should probs just do it in the shell script, tbh)
  29.         var lcd_value = Math.abs(Math.round(sys_info[mid_monitor]*1));
  30.         var big_light_value = Math.abs(Math.round(sys_info[bot_monitor]*1));
  31.         
  32.         //don't know which monitor is cpu, so...
  33.         if (top_monitor == 0) top_light_value = Math.round(top_light_value/num_procs);
  34.         if (mid_monitor == 0) lcd_value = Math.round(lcd_value/num_procs);
  35.         if (bot_monitor == 0) big_light_value = Math.round(big_light_value/num_procs);
  36.         
  37.         //alert('tl: ' + top_light_value + ", lcd: " + lcd_value + ", big: " + big_light_value);
  38.                         
  39.         var lcd_info = document.getElementById("lcd_info");
  40.         var top_light_imgs = document.getElementById("top_light").getElementsByTagName("img");
  41.         var big_light_imgs = document.getElementById("big_light").getElementsByTagName("img");
  42.         
  43.         document.getElementById("top_light").setAttribute("title", monitors[top_monitor] + ": " + top_light_value);
  44.         lcd_info.setAttribute("title", monitors[mid_monitor] + ": " + lcd_value);
  45.         document.getElementById("big_light").setAttribute("title", monitors[bot_monitor] + ": " + big_light_value + "%");
  46.         
  47.         //lcd first:
  48.         if (lcd_value < 100) {
  49.             lcd_info.style.fontSize = "30px";
  50.             lcd_info.style.top = "60px";
  51.             lcd_info.style.left = "50px";
  52.         } else {
  53.             lcd_info.style.fontSize = "23px";
  54.             lcd_info.style.top = "63px";
  55.             lcd_info.style.left = "47px";
  56.         }
  57.         
  58.         lcd_info.innerText = lcd_value;
  59.         document.getElementById("lcd_crisis").style.visibility = (lcd_value < 80) ? "hidden" : "visible";
  60.     
  61.         //now the top light:
  62.         if (monitors[top_monitor] == "Airport Strength" || monitors[top_monitor] == "Battery Level") top_light_value = 100 - (top_light_value*1); //since the higher the better for these...
  63.  
  64.         var top_light_vals = getOpacities(top_light_value*1);
  65.         
  66.         top_light_imgs[0].style.opacity = top_light_vals[0];
  67.         top_light_imgs[1].style.opacity = top_light_vals[1];
  68.         top_light_imgs[2].style.opacity = top_light_vals[2];
  69.         
  70.         //and finally the big light:
  71.         if (monitors[bot_monitor] == "Airport Strength" || monitors[bot_monitor] == "Battery Level") big_light_value = 100 - (big_light_value*1); //since the higher the better for these...
  72.  
  73.         var big_light_vals = getOpacities(big_light_value*1);
  74.         
  75.         big_light_imgs[0].style.opacity = big_light_vals[0];
  76.         big_light_imgs[1].style.opacity = big_light_vals[1];
  77.         big_light_imgs[2].style.opacity = big_light_vals[2];
  78.     
  79.         setTimeout("getSysInfo()", 2000); //update every two seconds
  80.     }
  81. }
  82.  
  83. function getOpacities(perc) {
  84.     //rather shoddy way of doing this, tbh:
  85.     var g, y, r;
  86.     
  87.     if (perc <= 20) {
  88.         g = 1.0;
  89.         y = 0.0;
  90.         r = 0.0;
  91.     } else if (perc > 20 && perc <= 50) {
  92.         g = 1- ((perc - 20)/30);
  93.         y = (perc - 20)/30;
  94.         r = 0.0;
  95.     } else if (perc > 50 && perc <= 80) {
  96.         g = 0.0;
  97.         y = 1- ((perc - 50)/30);
  98.         r = (perc - 50)/30;
  99.     } else {
  100.         g = 0.0;
  101.         y = 0.0;
  102.         r = 1.0;
  103.     }
  104.     
  105.     //alert("g: " + g + ", y: " + y + ", r: " + r);
  106.     
  107.     var ar = [g, y, r];
  108.     return ar;
  109. }
  110.  
  111. function togglePreferences() {
  112.     var f = document.getElementById("front");
  113.     var b = document.getElementById("back");
  114.     
  115.     if (b.style.display != "block") {
  116.         widgetAtFront = false;
  117.         document.getElementById("shutter").style.display = "block";
  118.  
  119.         //meh, this is fugly, but necessary:
  120.         info_anim.play('forward',
  121.             function() {
  122.                 b.style.display = "block";
  123.                 f.style.display = "none";
  124.     
  125.                 if (window.widget) {
  126.                     widget.prepareForTransition("ToBack");
  127.                      setTimeout("widget.performTransition()", 0);
  128.                 }
  129.                 setTimeout(
  130.                     function(){
  131.                         if (Statosphere) {
  132.                             if (Statosphere.isRegistered()) {
  133.                                 document.getElementById("not_registered").style.display = "none";
  134.                                 document.getElementById("registered").style.display = "block";
  135.                             } else {
  136.                                 document.getElementById("not_registered").style.display = "block";
  137.                                 document.getElementById("registered").style.display = "none";
  138.                             }
  139.                         }
  140.                     
  141.                         pref_anim.play('forward', 
  142.                              function() {
  143.                                  fade("preferences", 0, 1, null);
  144.                                  widget.setCloseBoxOffset(25,25);
  145.                              });
  146.                      }, 1000);
  147.             });
  148.     } else {
  149.         //and this looks even worse :/
  150.         fade("preferences", 1, 0,
  151.             function() {
  152.                 pref_anim.play('back',
  153.                     function() {
  154.                         b.style.display = "none";
  155.                         f.style.display = "block";
  156.             
  157.                         if (window.widget) {
  158.                             widget.prepareForTransition("ToFront");
  159.                              setTimeout("widget.performTransition()", 0);
  160.                         }
  161.                         setTimeout(function(){info_anim.play('back', 
  162.                             function() {
  163.                                 document.getElementById("shutter").style.display = "none";
  164.                                 widgetAtFront = true;
  165.                                 getSysInfo();
  166.                                 widget.setCloseBoxOffset(25,155);
  167.                             });}, 1000);
  168.                     });
  169.             });
  170.     }
  171. }
  172.  
  173.  
  174. function doneSettings() {
  175.     if (window.widget) {
  176.         //alert("----------------------------------")
  177.         //set prefs
  178.         var pref_imgs = document.getElementById("preferences").getElementsByTagName("img");
  179.         for (var i=0; i < pref_imgs.length-1; i++) {
  180.             if (pref_imgs[i].style.display != "none") {
  181.                 widget.setPreferenceForKey(pref_imgs[i].offsetTop, pref_imgs[i].id + "_top");
  182.                 widget.setPreferenceForKey(pref_imgs[i].offsetLeft, pref_imgs[i].id + "_left");
  183.                 widget.setPreferenceForKey(pref_imgs[i].getAttribute("docked"), pref_imgs[i].id + "_docked");
  184.                 //alert(pref_imgs[i].id + " : " + pref_imgs[i].getAttribute("docked"));
  185.             }
  186.         }
  187.     }
  188.  
  189.     togglePreferences();
  190. }
  191.  
  192. //mouseover stuff for the 'i' button:
  193.  
  194. var isShown = false;
  195.  
  196. function mouseOver(e) {
  197.     document.getElementById("flipper").style.visibility = "visible";
  198.     e.cancelBubble = true;
  199.  
  200.     if (e.target.parentNode.id == "flip" && !isShown) {
  201.         fade('flip_back', 0, 0.3, null);
  202.         isShown = true;
  203.     }
  204. }
  205.  
  206. function mouseOut(e) {
  207.     document.getElementById("flipper").style.visibility = "hidden";
  208.     e.cancelBubble = true;
  209.     
  210.     if (e.target.id != "flipper" && e.target.parentNode.id != "flip" && isShown) {
  211.         if (document.getElementById("flip").style.display != "none") {
  212.             fade('flip_back', 0.3, 0, null);
  213.         } else {
  214.             document.getElementById("flip_back").style.opacity = 0.0;
  215.             document.getElementById("flip").style.display = "";
  216.         }
  217.         isShown = false;
  218.     }
  219. }
  220.  
  221. function fade(el, from, to, callBack) {
  222.     //so we can pass a string for the id or an element:
  223.     if (el == new String(el)) el = document.getElementById(el);
  224.  
  225.     var dif = Math.abs(to - from);
  226.     
  227.     var pi = Math.PI;
  228.     var i = 0;
  229.     
  230.     //to gain a slight speed boost - work out all values first.
  231.     var opacs = [];
  232.     
  233.     for (var theta=0; theta < 90; theta+=8) { //play with the increment value for theta to get this smoother
  234.         if (to > from) 
  235.             var step = Math.sin((theta*pi)/180);
  236.         else 
  237.             var step = Math.cos((theta*pi)/180);
  238.         
  239.         opacs.push(dif*step);
  240.     }
  241.         
  242.     var inter = setInterval(
  243.         function() {
  244.             if (++i < opacs.length) {
  245.                 el.style.opacity = Math.max(0.01, opacs[i]); //fixing safari glitch
  246.             } else {
  247.                 el.style.opacity = to;
  248.  
  249.                 clearInterval(inter);
  250.                 if (callBack != null) callBack();
  251.             }
  252.         }, 100); //play with this interval value to get this smoother
  253. }
  254.  
  255. function slideTo(el, from_x, from_y, to_x, to_y, speed, callBack) {
  256.     if (el == new String(el)) el = document.getElementById(el);
  257.     
  258.     var x_dif = (to_x - from_x)/10;
  259.     var y_dif = (to_y - from_y)/10;
  260.     
  261.     var theta = 0;
  262.     
  263.     var inter = setInterval(
  264.         function() {
  265.             if (theta < 10) {
  266.                 theta++;
  267.                 
  268.                 from_x += x_dif;
  269.                 from_y += y_dif;
  270.                 
  271.                 el.style.left = from_x + "px";
  272.                 el.style.top = from_y + "px";
  273.             } else {
  274.                 el.style.left = to_x + "px";
  275.                 el.style.top = to_y + "px";
  276.                 
  277.                 clearInterval(inter);
  278.                 if (callBack != null) callBack();
  279.             }
  280.         }, speed);
  281. }
  282.  
  283. function Animation(id, what, strip_width, loops, interval) {
  284.     this.inter = null;
  285.     
  286.     this.instance = id; //i suppose this could possibly be done using random numbers instead, but...
  287.     eval(this.instance + " = this"); //giving us a reference to the object - I don't like eval in general, but this is an easy way of getting round this problem
  288.     
  289.     this.element = document.getElementById(what);
  290.     this.left = 0;
  291.     this.cur_loop = 0;
  292.     this.dx = this.element.offsetWidth + 10;
  293.     this.strip_width = strip_width;
  294.     this.loops = loops;
  295.     this.interval = interval;
  296.     this.isPlaying = false;
  297.     
  298.     this.end_point = 0;
  299.     
  300.     this.dir = "forward";
  301.     this.callBack = null;
  302.     
  303.     this.play = 
  304.         function(dir, callBack) {
  305.             if (!this.isPlaying) {
  306.                 this.isPlaying = true;
  307.                 this.dir = dir;
  308.                 this.callBack = callBack;
  309.                 
  310.                 if (this.dir == "back") {
  311.                     this.end_point =  this.strip_width * -1;
  312.                     this.left = 10;
  313.                 } else {
  314.                     this.end_point = 0;
  315.                     this.left = this.strip_width;
  316.                 }
  317.                 
  318.                 //alert('dx: ' + this.dx);
  319.                 
  320.                 this.inter = setInterval(this.instance + ".roll()", this.interval);
  321.             }
  322.         }
  323.     
  324.     this.stop = 
  325.         function () {
  326.             clearInterval(this.inter);
  327.             this.isPlaying = false;
  328.         }
  329.         
  330.     this.roll =
  331.         function() {
  332.             this.left -= this.dx;
  333.             
  334.             //document.getElementById("debug").innerHTML += this.left + "<br />";
  335.             
  336.             if (this.left >= this.end_point) {
  337.                 var pos = (this.dir == "back") ? this.left * -1 : this.left;
  338.                 this.element.style.backgroundPosition = pos + "px 0";
  339.             } else {
  340.                 if (this.loops > 0 && ++this.cur_loop >= this.loops) {
  341.                     clearInterval(this.inter);
  342.                     this.cur_loop = 0;
  343.                     this.isPlaying = false;
  344.                     if (this.callBack != null) {
  345.                         this.callBack();
  346.                         this.callBack = null;
  347.                     }
  348.                 } else {
  349.                     this.element.style.backgroundPosition = "0 0";
  350.                 }
  351.             }
  352.         }
  353. }
  354.  
  355. /*
  356.     OK. Since there's only ever going to be one thing dragged at a time,
  357.     we can make use of a global variable to keep track of the current
  358.     object being dragged and also add the event listener to the body so we
  359.     can drag it all over without having problems if the object doesn't follow
  360.     the mouse fast enough...
  361.  */
  362.  
  363. var cur_drag_obj = null;
  364.  
  365. function DraggableElement(what, id, controller, endFunction) {
  366.     this.element = (what == new String(what)) ? document.getElementById(what) : what;
  367.     
  368.     this.instance = id;
  369.     eval(this.instance + " = this");
  370.     
  371.     this.controller = controller;
  372.     this.endFunction = endFunction;
  373.     
  374.     this.canmove = false;
  375.     this.init_y = 0;
  376.     this.init_x = 0;
  377.     this.init_top = 0;
  378.     this.init_left = 0;
  379.  
  380.     this.mousemove = function(e) {
  381.         if (this.canmove) {
  382.             this.top = e.clientY - this.init_y;
  383.             this.left = e.clientX - this.init_x;
  384.             
  385.             //so, controller is actually the controlling function. This
  386.             //means we can use this 'class' for multiple things,
  387.             //such as scrollbars or sliders etc.
  388.             
  389.             if (this.controller != null) this.controller();
  390.         }
  391.     }
  392.     
  393.     this.mousedown = function(e) {
  394.         this.element.style.zIndex = 99;
  395.         this.init_top = this.element.offsetTop;
  396.         this.init_left = this.element.offsetLeft;
  397.         
  398.         this.init_y = (e.clientY - this.init_top);
  399.         this.init_x = (e.clientX - this.init_left);
  400.         this.canmove = true;
  401.  
  402.     //the following seems rather 'hacky' doesn't it?
  403.  
  404.         cur_drag_obj = this;
  405.         document.body.addEventListener("mousemove", dragging, false);
  406.         document.body.addEventListener("mouseup", finishDragging, false);
  407. }
  408.     
  409.     this.mouseup = function(e) {
  410.         this.element.style.zIndex = 1;
  411.         this.canmove = false;
  412.         
  413.         if (this.endFunction != null) this.endFunction();
  414.  
  415.         //and remove the event listeners:        
  416.         document.body.removeEventListener("mousemove", dragging, false);
  417.         document.body.removeEventListener("mouseup", finishDragging, false);
  418.     }
  419.     
  420.     this.element.setAttribute("onmousedown", this.instance + ".mousedown(event)");
  421. }
  422.  
  423. /*
  424.     note that I had to actually make these to as proper functions for the event
  425.     listeners, otherwise they weren't being removed (when using something like
  426.     function(e){cur_drag_obj.mousemove(e);} instead).
  427.  */
  428. function dragging(e) {
  429.     cur_drag_obj.mousemove(e);
  430. }
  431.  
  432. function finishDragging(e) {
  433.     cur_drag_obj.mouseup(e);
  434. }
  435.  
  436. /*
  437.     I seriously need to give the following function a bit more thought :/
  438.  */
  439.  
  440. function dockElement(el, where) {
  441.     var docked, node;
  442.     
  443. //now make them dock at the pref points:
  444.     if (this.left >= 36 && this.left <= 55) {
  445.         if (this.top >= 11 && this.top <= 21) {
  446.             this.top = 15;
  447.             this.left = 42;
  448.             docked = "top";
  449.         } else if (this.top >= 38 && this.top <= 55) {
  450.             this.top = 44;
  451.             this.left = 42;
  452.             docked = "middle";
  453.         } else if (this.top >= 65 && this.top <= 79) {
  454.             this.top = 71;
  455.             this.left = 42;
  456.             docked = "bottom";
  457.         }
  458.     } else {
  459.         docked = "no"; //this is temporary until i decide whether there *has* to be something in all of the three slots
  460.     }
  461.     
  462. //run down the images on the back and undock ones that are docked where we want to be:
  463. //we'll also get what node we are, to add it to the pos_monitor vars
  464.     var pref_imgs = document.getElementById("preferences").getElementsByTagName("img");
  465.     for (var i=0; i < pref_imgs.length-1; i++) {
  466.         if (pref_imgs[i] != this.element && pref_imgs[i].offsetTop == this.top && pref_imgs[i].offsetLeft == this.left) {
  467.             var swapped_dock = this.element.getAttribute("docked")
  468.             pref_imgs[i].setAttribute("docked", swapped_dock);
  469.             
  470.             switch (swapped_dock) {
  471.                 case "top":
  472.                     top_monitor = i;
  473.                     break;
  474.                 case "middle":
  475.                     mid_monitor = i;
  476.                     break;
  477.                 case "bottom":
  478.                     bot_monitor = i;
  479.                     break;
  480.             }
  481.     
  482.             //pref_imgs[i].style.top = this.init_top + "px";
  483.             //pref_imgs[i].style.left = this.init_left + "px";
  484.             slideTo(pref_imgs[i], this.left, this.top, this.init_left, this.init_top, 10, null);
  485.             //break;
  486.         }
  487.         
  488.         if (pref_imgs[i] == this.element) node = i;
  489.     }
  490.         
  491.     switch (docked) {
  492.         case "top":
  493.             top_monitor = node;
  494.             break;
  495.         case "middle":
  496.             mid_monitor = node;
  497.             break;
  498.         case "bottom":
  499.             bot_monitor = node;
  500.             break;
  501.     }
  502.     
  503.     this.element.style.top = this.top + "px";
  504.     this.element.style.left = this.left + "px";
  505.     
  506.     //and now set our elements docked attribute to reflect it's new place:
  507.     this.element.setAttribute("docked", docked);
  508.     
  509.     //alert("top: " + top_monitor + ", mid: " + mid_monitor + ", bot: " + bot_monitor);
  510. }
  511.  
  512. dockElement.prototype = DraggableElement;
  513.  
  514. function moveElement() {
  515.     //keep them within the bounds of the prefs area:
  516.     if (this.top <= 0) this.top = 0;
  517.     if (this.top >= 95) this.top = 95;
  518.     if (this.left <= 0) this.left = 0;
  519.     if (this.left >= 81) this.left = 81;
  520.     
  521.     this.element.style.top = this.top + "px";
  522.     this.element.style.left = this.left + "px";
  523. }
  524.  
  525. moveElement.prototype = DraggableElement;
  526.  
  527. function toggleRunning(e) {
  528.     if (e.altKey) {
  529.         widgetIsShowing = !widgetIsShowing;
  530.     }
  531. }
  532.  
  533. function getOffsetTop(element) {
  534.     var top = 0;
  535.     var el = element;
  536.     
  537.     do {
  538.         top += el.offsetTop;
  539.         el = el.parentNode;
  540.     } while (el !== document.body);
  541.     
  542.     return top;
  543. }
  544.  
  545. function getOffsetLeft(element) {
  546.     var left = 0;
  547.     var el = element;
  548.     
  549.     do {
  550.         left += el.offsetLeft;
  551.         el = el.parentNode;
  552.     } while (el !== document.body);
  553.     
  554.     return left;
  555. }
  556.  
  557. var help_running = false;
  558.  
  559. function showHelp() {
  560.     //this is basically an animation showing how to use the widget:
  561.     help_running = true;
  562.     
  563.     //get a non docked element and the element docked at the top:
  564.     var top_docked, non_docked;
  565.     
  566.     var pref_imgs = document.getElementById("preferences").getElementsByTagName("img");
  567.     for (var i=0; i < pref_imgs.length-2; i++) { //taking into account the done button
  568.         if (pref_imgs[i].getAttribute("docked") == "top") top_docked = pref_imgs[i];
  569.         if (pref_imgs[i].getAttribute("docked") == "no") non_docked = pref_imgs[i];
  570.     }
  571.  
  572.     //HAHA - the following is almost a joke, isn't it?
  573.     
  574.     document.getElementById("cover").style.display = "block";
  575.     showHelpText(0,
  576.     function () {
  577.         circleObject(non_docked,
  578.         function () {
  579.             hideHelpText(0, 
  580.             function () {
  581.                 circleObject(top_docked, 
  582.                 function () {
  583.                     var l = non_docked.offsetLeft;
  584.                     var t = non_docked.offsetTop;
  585.                     slideTo(non_docked, l, t, 42, 15, 100, 
  586.                     function () {
  587.                         slideTo(top_docked, 42, 15, l, t, 100, 
  588.                         function () {
  589.                             showHelpText(1,
  590.                             function () {
  591.                                 circleObject('done_preferences',
  592.                                 function () {
  593.                                     hideHelpText(1, 
  594.                                     function () {
  595.                                         showHelpText(2,
  596.                                         function () {
  597.                                             setTimeout(
  598.                                             function() {
  599.                                                 hideHelpText(2, 
  600.                                                 function () {
  601.                                                     fade("help_circle_canvas", 1.0, 0, function() {document.getElementById("help_circle_canvas").style.opacity = 1.0; document.getElementById("help_circle_canvas").style.display = "none";});
  602.                                                     togglePreferences();
  603.                                                     setTimeout(
  604.                                                     function() {
  605.                                                         //put the moved items on the back back to where they were:
  606.                                                         slideTo(top_docked, l, t, 42, 15, 100, null);
  607.                                                         slideTo(non_docked, 42, 15, l, t, 100, null);
  608.                                                         document.getElementById("help_text").style.top = "170px";
  609.                                                         circleObject('top_light',
  610.                                                         function () {
  611.                                                             showHelpText(3,
  612.                                                             function () {
  613.                                                                 setTimeout(
  614.                                                                 function() {
  615.                                                                     hideHelpText(3, 
  616.                                                                     function () {
  617.                                                                         showHelpText(4,
  618.                                                                         function () {
  619.                                                                             setTimeout(
  620.                                                                             function() {
  621.                                                                                 hideHelpText(4, 
  622.                                                                                 function () {
  623.                                                                                     showHelpText(5,
  624.                                                                                     function () {
  625.                                                                                         setTimeout(
  626.                                                                                         function() {
  627.                                                                                             hideHelpText(5, 
  628.                                                                                             function () {
  629.                                                                                                 document.getElementById("help_circle_canvas").style.display = "none";
  630.                                                                                                 document.getElementById("cover").style.display = "none";
  631.                                                                                                 document.getElementById("help_text").style.top = "100px";
  632.                                                                                                 togglePreferences();
  633.                                                                                             }); //hideHelpText 5
  634.                                                                                         }, 3000);
  635.                                                                                     });//showHelpText 5
  636.                                                                                 });//hideHelpText 4
  637.                                                                             }, 3000);
  638.                                                                         });//showHelpText 4
  639.                                                                     });//hideHelpText 3
  640.                                                                 }, 3000);
  641.                                                             });//howHelpText 3
  642.                                                         });//circleObject 'top_light'
  643.                                                     }, 3500);
  644.                                                 });//hideHelpText 2
  645.                                             }, 3000);
  646.                                         });//showHelpText 2
  647.                                     });//hideHelpText 1
  648.                                 });//circleObject 'done_preferences'
  649.                             });//showHelpText 1
  650.                         });//slideTo
  651.                     });//slideTo
  652.                 });//circleObject top_docked
  653.             });//hideHelpText 0
  654.         });//circleObject non_docked
  655.     });//showHelpText 0
  656. }
  657.  
  658. function cancelHelp() {
  659.     help_running = false;
  660.     document.getElementById("help_circle_canvas").style.display = "none";
  661.     document.getElementById("help_text").style.display = "none";
  662.     document.getElementById("cover").style.display = "none";
  663.     document.getElementById("help_text").style.top = "100px";
  664. }
  665.  
  666. function handleKeyPress(e) {
  667.     if (e.charCode == 27 && help_running) cancelHelp();
  668. }
  669.  
  670. function circleObject(obj, callBack) {
  671.     if (!help_running) return false;
  672.     
  673.     if (obj == new String(obj)) obj = document.getElementById(obj);
  674.     
  675.     var help_canvas = document.getElementById("help_circle_canvas");
  676.     var help_context = help_canvas.getContext("2d");
  677.     
  678.     help_canvas.style.display = "block";
  679.     
  680.     help_canvas.style.top = (getOffsetTop(obj) - 5) + "px";
  681.     help_canvas.style.left = (getOffsetLeft(obj) - 5) + "px";
  682.     //help_canvas.setAttribute("height", 200);
  683.     //help_canvas.setAttribute("width", 200);
  684.     /*help_canvas.setAttribute("height", obj.offsetHeight + 20);
  685.     help_canvas.setAttribute("width", obj.offsetWidth + 20);
  686.     */
  687.     
  688.     help_context.lineWidth = 3;
  689.     help_context.strokeStyle = "rgba(255, 0, 0, 0.8)";
  690.     
  691.     help_context.clearRect(0, 0, 50, 50);
  692.     help_context.save();
  693.         
  694.     help_context.scale(1.3, 1);
  695.         
  696.     help_context.beginPath();
  697.     help_context.moveTo(5, 10);
  698.     
  699.     var i=0;
  700.     
  701.     var inter = setInterval(
  702.         function () {
  703.             //alert(i);
  704.             switch (i) {
  705.                 case 0:
  706.                     help_context.bezierCurveTo(5, 0, 20, 10, 20, 20);
  707.                     break;
  708.                 case 1:
  709.                     help_context.beginPath();
  710.                     help_context.moveTo(20, 10);
  711.                     help_context.bezierCurveTo(30, 20, 20, 30, 15, 20);
  712.                     break;
  713.                 case 2:
  714.                     help_context.beginPath();
  715.                     help_context.moveTo(20, 30);
  716.                     help_context.bezierCurveTo(10, 35, 5, 25, 5, 10);
  717.                     break;
  718.                 case 3:
  719.                     help_context.beginPath();
  720.                     help_context.moveTo(5, 25);
  721.                     help_context.bezierCurveTo(2, 7, 9, 10, 0, 0);
  722.                     break;
  723.                 //Default:
  724.                 case 4:
  725.                     clearInterval(inter);
  726.                     help_context.restore();
  727.                     if (callBack != null) callBack();
  728.                     break;
  729.             }
  730.             help_context.stroke();
  731.             i++;
  732.         }, 70);
  733. }
  734.  
  735. function showHelpText(which, callBack) {
  736.     if (!help_running) return false;
  737.     
  738.     var help_text = document.getElementById("help_text");
  739.     help_text.style.display = "block";
  740.     
  741.     var messages = help_text.getElementsByTagName("div");
  742.     if (which > 0) messages[which-1].style.opacity = 0;
  743.     
  744.     fade(messages[which], 0, 0.9, 
  745.         function() {
  746.             //setTimeout("help_text_shadow.paint()", 100);
  747.             callBack();
  748.         });
  749. }
  750.  
  751. function hideHelpText(which, callBack) {
  752.     var help_text = document.getElementById("help_text");
  753.     
  754.     
  755.     var messages = help_text.getElementsByTagName("div");
  756.     //if (which > 0) messages[which-1].style.opacity = 0;
  757.     
  758.     fade(messages[which], 0.9, 0, 
  759.         function () {
  760.             help_text.style.display = "none";
  761.             callBack();
  762.         });
  763. }
  764.  
  765. function ShadowedElement(what) {
  766.     this.element = (what == new String(what)) ? document.getElementById(what) : what;
  767.     
  768. //add in shadow:
  769.     this.canvas_container = document.createElement("div");
  770.     this.canvas_container.className = "shadow_container";
  771.     
  772.     
  773.     this.element.insertBefore(this.canvas_container, this.element.firstChild);
  774.     
  775.     this.canvas = document.createElement("canvas");
  776.     this.canvas.setAttribute("height", this.canvas_container.offsetHeight); //seems it doesn't scale 100% to the actual width of it - the width/height is extended by padding, y'see
  777.     this.canvas.setAttribute("width", this.canvas_container.offsetWidth);
  778.     
  779.     this.canvas_container.appendChild(this.canvas);
  780.     
  781.     this.context = this.canvas.getContext("2d");
  782.     
  783.     this.paint = function() {
  784.         //alert('painting shadow');
  785.         this.context.shadowBlur = 7;
  786.         this.context.shadowOffsetX = 0;
  787.         this.context.shadowOffsetY = 3;
  788.         this.context.lineWidth = 10;
  789.     
  790.         this.context.beginPath();
  791.         this.context.strokeRect(14, 14, this.element.offsetWidth-10, this.element.offsetHeight-10);
  792.     }
  793.     
  794.     this.paint();
  795. }
  796. var help_running_order = [];
  797.  
  798. //animation objects:
  799. var pref_anim; //opening and closing of the preferences
  800. var info_anim; //the circle on the front opening and closing
  801. //var help_text_shadow;
  802.  
  803. function register() {
  804.     if (Statosphere) Statosphere.registerWidget();
  805. }
  806.  
  807. function buy() {
  808.     if (Statosphere) Statosphere.buyWidget();
  809. }
  810.  
  811. function init() {
  812.     if (window.widget) {
  813.         window.resizeTo(139, 291);
  814.     
  815.         widget.setCloseBoxOffset(25,155);
  816.     
  817.         //get preferences:
  818.         
  819.         //check to see if they have a battery (they're using an [i|power]Book):
  820.         var has_battery = widget.preferenceForKey("has_battery");
  821.         if (!has_battery) { //not false, but undefined
  822.             //this is the first run of the widget, so check what type of mac it is:
  823.             var model = widget.system("/usr/sbin/sysctl hw.model", null).outputString;
  824.             has_battery = (model.indexOf("PowerBook") != -1) ? "true" : "false";
  825.             widget.setPreferenceForKey(has_battery, "has_battery");
  826.         }
  827.         if (has_battery == "false") document.getElementById("battery").style.display = "none";
  828.  
  829.         //and now see if they have an airport card:
  830.     }
  831.     
  832.     //set up the flip ('i') button:
  833.     var bod = document.getElementsByTagName("body")[0];
  834.     bod.addEventListener("mouseover", mouseOver, false);
  835.     bod.addEventListener("mouseout", mouseOut, false);
  836.     document.getElementById("flip").addEventListener("mouseup", function(e){document.getElementById("flip").style.display="none"; togglePreferences();}, false);
  837.  
  838.     //set up animation objects:
  839.     pref_anim = new Animation('pref_anim', 'back', 2970, 1, 10);
  840.     info_anim = new Animation('info_anim', 'shutter', 1286, 1, 10);
  841.     
  842.     /*
  843.         and set up the preference elements to be able to move:
  844.         since we make our own reference to the object instance in the object
  845.         it doesn't matter what variables we use here (go local vars! hehe)
  846.      */
  847.  
  848.     var pref_imgs = document.getElementById("preferences").getElementsByTagName("img");
  849.     for (var i=0; i < pref_imgs.length-2; i++) { //taking into account the done button and help button
  850.         if (pref_imgs[i].style.display != "none") {
  851.             if (window.widget) {
  852.                 var top = widget.preferenceForKey(pref_imgs[i].id + "_top");
  853.                 var left = widget.preferenceForKey(pref_imgs[i].id + "_left");
  854.                 
  855.                 pref_imgs[i].style.top = top + "px";
  856.                 pref_imgs[i].style.left = left + "px";
  857.                 
  858.                 var docked = widget.preferenceForKey(pref_imgs[i].id + "_docked");
  859.                 //alert(pref_imgs[i].id + "'s docked: " + docked);
  860.                 if (docked) {
  861.                     pref_imgs[i].setAttribute("docked", docked);
  862.                     if (docked == "top") top_monitor = i; //pref_imgs[i].id;
  863.                     if (docked == "middle") mid_monitor = i; //pref_imgs[i].id;
  864.                     if (docked == "bottom") bot_monitor = i; //pref_imgs[i].id;
  865.                 }
  866.             }
  867.             var tmp = new DraggableElement(pref_imgs[i], "move_"+i, moveElement, dockElement);
  868.         }
  869.     }
  870.     
  871.     //now change the back to have no display:
  872.     document.getElementById("back").style.display = "none";
  873.     document.getElementById("back").style.visibility = "visible";
  874.         
  875.     //doing it this way to stop any flickering (well, attempt to anyway).
  876.     document.getElementById("front").style["background"] = "url(images/ball.png) no-repeat";
  877.         
  878.     info_anim.play('back',
  879.         function(){
  880.             document.getElementById("shutter").style.display = "none";
  881.             document.getElementsByTagName("body")[0].addEventListener("click", toggleRunning, true);
  882.  
  883.             if (window.widget) {
  884.                 //make sure our script is executable:
  885.                 widget.system("/bin/chmod 777 sysinfo.sh", null);
  886.                 //and the aiport prog:
  887.                 widget.system("/bin/chmod 777 airport", null);
  888.                 
  889.                 //figure out how many processors they have:
  890.                 num_procs = parseInt(widget.system("/usr/sbin/sysctl -n hw.ncpu", null).outputString);
  891.  
  892.                 setTimeout("getSysInfo()", 500);
  893.             }
  894.         });
  895.         
  896.     //help_text_shadow = new ShadowedElement('help_text')
  897.     document.addEventListener("keydown", handleKeyPress, false);
  898. }
  899.  
  900. window.onload = init;
  901. if (window.widget) {
  902.     widget.onhide = function(){widgetIsShowing = false;};
  903.     widget.onshow = function(){widgetIsShowing = true; getSysInfo();};
  904. }